package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.security.annotation.Inner;
import com.workcell.workdata.xthings.api.feign.RemoteIotOpenService;
import com.workcell.workdata.xthings.dto.XthingsTvVo;
import com.workcell.workdata.xthings.service.XthingsTvAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author 廖博文
 * @Date 2023/1/10 17:17
 * @Description xthingstv
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/xthingstvapp" )
@Api(value = "xthingstvapp", tags = "电视机TvAPP")
public class XthingsTvAppController {

	private final XthingsTvAppService xthingsTvAppService;
	private final RemoteIotOpenService remoteIotOpenService;

	@ApiOperation(value = "服务端投看板：扫码", notes = "服务端投看板：扫码")
	@GetMapping("/saveTvIdentOfServer" )
	public R saveTvIdentOfServer(String deviceNo){
		Assert.isTrue(StringUtils.isNotEmpty(deviceNo),"序列号不能为空");
		return xthingsTvAppService.saveTvIdentOfServer(deviceNo);
	}

	@ApiOperation(value = "服务端：查询该人看板配置", notes = "服务端：查询该人看板配置")
	@GetMapping("/getTvBulletinBoardConfOfServer" )
	public R getTvBulletinBoardConfOfServer(String personnel){
		Assert.isTrue(StringUtils.isNotEmpty(personnel),"人员ID不能为空");
		return xthingsTvAppService.getTvBulletinBoardConfOfServer(personnel);
	}

	@ApiOperation(value = "服务端：发送选择的看板内容", notes = "服务端：发送选择的看板内容")
	@GetMapping("/sendTvBulletinBoardOfServer" )
	public R sendTvBulletinBoardOfServer(String deviceNo,String url){
		Assert.isTrue(StringUtils.isNotEmpty(deviceNo),"序列号不能为空");
		Assert.isTrue(StringUtils.isNotEmpty(url),"访问地址不能为空");
		return xthingsTvAppService.sendTvBulletinBoardOfServer(deviceNo, url);
	}

	@ApiOperation(value = "语音播报：指定发送", notes = "语音播报：指定发送")
	@GetMapping("/sendVoicePlaybackOne")
	public R sendVoicePlaybackOne(String fromUserId,String toUserId,String messageText){
		Map<String,String> map = new HashMap<>(3);
		map.put("fromUserId",fromUserId);
		map.put("toUserId",toUserId);
		map.put("messageText",messageText);
		return remoteIotOpenService.sendVoicePlaybackOne(map);
	}

	@ApiOperation(value = "语音播报：全体发送", notes = "语音播报：全体发送")
	@GetMapping("/sendVoicePlaybackAll" )
	public R sendVoicePlaybackAll(String fromUserId,String toUserId,String messageText){
		Map<String,String> map = new HashMap<>(3);
		map.put("fromUserId",fromUserId);
		map.put("toUserId",toUserId);
		map.put("messageText",messageText);
		return remoteIotOpenService.sendVoicePlaybackAll(map);
	}

	@ApiOperation(value = "语音播放：在线人数", notes = "语音播报：全体发送")
	@GetMapping("/getOnLineAllUser" )
	public R getOnLineAllUser(){
		return remoteIotOpenService.getOnLineAllUser();
	}

	@ApiOperation(value = "获取smartOS看板列表", notes = "获取smartOS看板列表")
	@GetMapping("/getSmartOSBoardlist" )
	public R getSmartOSBoardlist(String name, String deviceNo, Integer size, Long userId){
		Assert.isTrue(StringUtils.isNotEmpty(name),"应用名称不能为空");
		Assert.isTrue(StringUtils.isNotEmpty(deviceNo),"设备编号SN不能为空");
		Assert.isTrue(ObjectUtil.isNotEmpty(userId),"用户ID不能为空");
		return xthingsTvAppService.getSmartOSBoardlist(name, deviceNo, size, userId);
	}

	@ApiOperation(value = "更新看板(支持多选轮播)", notes = "更新看板(支持多选轮播)")
	@PutMapping("/updateBoard" )
	public R updateBoard(@RequestBody @Validated XthingsTvVo.BoardVo boardVo){
		return xthingsTvAppService.updateBoard(boardVo);
	}

	@ApiOperation(value = "关联SN码：扫码", notes = "关联SN码：扫码")
	@GetMapping("/saveSnScanCode" )
	public R saveSnScanCode(String deviceId){
		Assert.isTrue(StringUtils.isNotEmpty(deviceId),"设备ID不能为空");
		return xthingsTvAppService.saveSnScanCode(deviceId);
	}

	@ApiOperation(value = "关联SN码(绑定设备ID和设备编码SN);绑定租户", notes = "关联SN码(绑定设备ID和设备编码SN);绑定租户")
	@PostMapping("/related" )
	public R related(@RequestBody @Validated XthingsTvVo.UserPickRentVo userPickRentVo){
		return xthingsTvAppService.relatedDiveceNo(userPickRentVo);
	}

	@ApiOperation(value = "是否绑定租户", notes = "是否绑定租户")
	@GetMapping("/tvIsBind" )
	public R tvIsBind(String deviceNo){
		Assert.isTrue(StringUtils.isNotEmpty(deviceNo),"设备编码SN不能为空");
		return xthingsTvAppService.tvIsBind(deviceNo);
	}

	@Inner
	@ApiOperation(value = "是否绑定租户", notes = "是否绑定租户")
	@GetMapping("/getTenant" )
	public R<Long> getTenant(@RequestParam("deviceNo")String deviceNo){
		Assert.isTrue(StringUtils.isNotEmpty(deviceNo),"设备编码SN不能为空");
		return xthingsTvAppService.tvIsBind(deviceNo);
	}
}
